package com.sidneysimmons.financially;

import java.util.regex.Pattern;

import com.sidneysimmons.financially.utils.StringUtil;

import android.util.Patterns;

public class Validator {
	
	/**
	 * Validates an email address.  Null or empty strings return false.
	 * 
	 * @param emailAddress - the email address to check
	 * @return true if validation passed, false otherwise
	 */
	public static boolean validateEmailAddress(String emailAddress) {
		if (StringUtil.isBlankOrNull(emailAddress)) {
			return false;
		}
		
		if (Patterns.EMAIL_ADDRESS.matcher(emailAddress).matches()) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Validates a password.  Null or empty strings return false.  Password 
	 * can only contain letters (upper or lower case) and numbers.  Number of 
	 * digits is limited to between 8 and 20.
	 * 
	 * @param password - the password to check
	 * @return true if validation passed, false otherwise
	 */
	public static boolean validatePassword(String password) {
		Pattern passwordPattern = Pattern.compile("\\w{8,20}");
		
		if (StringUtil.isBlankOrNull(password)) {
			return false;
		}
		
		if (passwordPattern.matcher(password).matches()) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * Validates the name of a Savings Account.  Null or empty strings return 
	 * false.  Account name can only contain letters (upper or lower case), 
	 * numbers, and spaces.  Number of digits limited to between 1 and 20.
	 * 
	 * @param accountName - the account name to check
	 * @return true if validation passed, false otherwise
	 */
	public static boolean validateAccountName(String accountName) {
		Pattern accountNamePattern = Pattern.compile("[0-9a-zA-Z ']{1,20}");
		
		if (StringUtil.isBlankOrNull(accountName)) {
			return false;
		}
		
		if (accountNamePattern.matcher(accountName).matches()) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Validates the name of a Category.  Null or empty strings return 
	 * false.  Account name can only contain letters (upper or lower case), 
	 * numbers, and spaces.  Number of digits limited to between 1 and 20.
	 * 
	 * @param accountName - the account name to check
	 * @return true if validation passed, false otherwise
	 */
	public static boolean validateCategoryName(String categoryName) {
		Pattern categoryNamePattern = Pattern.compile("[0-9a-zA-Z ']{1,20}");

		if (StringUtil.isBlankOrNull(categoryName)) {
			return false;
		}

		if (categoryNamePattern.matcher(categoryName).matches()) {
			return true;
		} else {
			return false;
		}
	}
}